package com.easylinkin.linkappapi.messagecenter.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.messagecenter.entity.MessageCenter;
import com.easylinkin.linkappapi.messagecenter.entity.vo.MessageCenterVo;
import com.easylinkin.linkappapi.messagecenter.service.MessageCenterService;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * MessageCenter表控制层
 *
 * @author CodeGenerator
 * @date 2022/07/11
 */
@RestController
@RequestMapping("appMessageCenter")
public class MessageCenterController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private MessageCenterService appMessageCenterService;

    /**
     * 分页查询所有数据
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("getPage")
    @ApiOperation("查询分页")
    public RestMessage selectPage(@RequestBody RequestModel<MessageCenter> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<MessageCenter> record = appMessageCenterService.selectPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }

    @PostMapping("count")
    @ApiOperation("查询未读数")
    public RestMessage count(@RequestBody MessageCenter messageCenter) {
        int count =  appMessageCenterService.count(messageCenter);
        return RestBuilders.successBuilder(count).build();
    }

    @PostMapping("countByType")
    @ApiOperation("查询未读数")
    public RestMessage countByType(@RequestBody MessageCenter messageCenter) {
        List<MessageCenterVo> messageCenterVos = appMessageCenterService.countByType(messageCenter);
        return RestBuilders.successBuilder(messageCenterVos).build();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("查询单条")
    public RestMessage selectOne(@PathVariable Serializable id) {
        return RestBuilders.successBuilder((this.appMessageCenterService.getOneById(id))).build();
    }

    /**
     * 新增数据
     *
     * @param appMessageCenter 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增")
    public RestMessage insert(@RequestBody MessageCenter appMessageCenter) {
        return RestBuilders.successBuilder().success((this.appMessageCenterService.saveOne(appMessageCenter))).build();
    }

    /**
     * 生成配电箱巡检消息
     *
     * @return
     */
    @PostMapping("test/electricCheckMessageGenScheduleTest")
    @ApiOperation("新增")
    public RestMessage electricCheckMessageGenScheduleTest() {
        this.appMessageCenterService.electricCheckMessageGenSchedule();
        return RestBuilders.successBuilder().build();
    }

    /**
     * 修改数据
     *
     * @param appMessageCenter 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation("修改单条")
    public RestMessage update(@RequestBody MessageCenter appMessageCenter) {
        return RestBuilders.successBuilder().success(this.appMessageCenterService.updateOne(appMessageCenter)).build();
    }

    /**
     * @param toStatus 需要改变成的状态
     * @return 修改结果
     */
    @PostMapping("updateStatusBatch")
    @ApiOperation("批量修改状态")
    public RestMessage updateStatusBatch(@RequestParam("toStatus") Integer toStatus,@RequestParam(value = "type",required = false)Integer type) {
        return RestBuilders.successBuilder().success(this.appMessageCenterService.updateStatusBatch(toStatus,type)).build();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("批量删除")
    public RestMessage delete(@RequestParam("idList") List<Long> idList) {
        return RestBuilders.successBuilder().success(this.appMessageCenterService.deleteByIds(idList)).build();
    }

    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody MessageCenter appMessageCenter, HttpServletRequest request, HttpServletResponse response) {
        appMessageCenterService.export(appMessageCenter, request, response);
    }

    /**
     * 根据id查询信息详情
     * @param id 主键
     * @return 信息
     */
    @GetMapping("/findMsgDtoById/{id}")
    @ApiOperation("查询单条信息中心信息")
    RestMessage findMsgDtoById(@PathVariable Long id) {
        return this.appMessageCenterService.findMsgDtoById(id);
    }
}

